/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.manager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * <p>Project: jse2204 - Util
 * <p>Powered by webrx On 2022-10-08 10:19:44
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Util {
    public final static String db = "dbsm.dat";
    public static List<Student> sm = new ArrayList<>();

    public static void save() {
        //保存数据 ObjectInputStream
        try (var ois = new ObjectOutputStream(new FileOutputStream(db))) {
            ois.writeObject(sm);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {

        }
    }

    public static void load() {
        File dd = new File(db);
        if (dd.exists()) {
            //读取数据 ObjectInputStream
            try (var ois = new ObjectInputStream(new FileInputStream(db))) {
                sm = (List<Student>) ois.readObject();
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (Exception e) {
            }
        } else {
            save();
        }


    }


    /**
     * Description: 实现录入学生的信息，并保存到集合中
     *
     * @date: 2022/10/8 10:20
     * @params: []
     * @return: void
     */
    public static void add() {
        System.out.println(Util.center(" 添加 ", 20, "*"));
        Scanner sc = new Scanner(System.in);
        Student st = new Student();


        System.out.print("请输入学号，是一个整数:");
        st.setSid(sc.nextInt());
        System.out.print("请输入姓名:");
        st.setSname(sc.next());
        System.out.print("请输入性别[男|女]:");
        st.setSgender(sc.next());
        System.out.print("请输入年龄:");
        st.setSage(sc.nextInt());
        System.out.print("请输入家庭地址:");
        st.setAddress(sc.next());
        sm.add(st);
        save();
        try {
            System.out.println("保存...");
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.printf("学生：%s，信息录入保存成功！！！%n%n%n", st.getSname());
    }

    public static void printAll() {
        load();
        System.out.println(Util.center(" 查看所有学生 ", 20, "*"));
        System.out.println(Util.left("学号", 10) + Util.center("姓名", 8, " ") + Util.left("家庭地址", 30));
        System.out.println("-".repeat(50));
        sm.forEach(s -> {
            System.out.println(Util.left("" + s.getSid(), 10) + Util.center(s.getSname(), 8, " ") + Util.left(s.getAddress(), 30));
        });
        System.out.println("-".repeat(50));
        System.out.printf("共有学生人数：%d%n%n%n%n", sm.size());
    }

    public static String left(String str, int length) {
        return String.format("%-" + length + "s", str);
    }

    public static String right(String str, int length) {
        return String.format("%" + length + "s", str);
    }


    public static String center(String str, int length, String ch) {
        int size = (length - str.length()) / 2;
        return ch.repeat(size).concat(str).concat(ch.repeat(size));
    }

    /**
     * Description: 实现删除学生信息
     *
     * @date: 2022/10/8 11:41
     * @params: [java.util.List<cn.webrx.manager.Student>]
     * @return: void
     */
    public static void delete() {
        System.out.println(Util.center(" 删除 ", 20, "*"));
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入删除的学生学号：");
        int sno = sc.nextInt();
        boolean f = false;
        for (int i = 0; i < sm.size(); i++) {
            Student st = sm.get(i);
            if (st.getSid() == sno) {
                f = true;
                sm.remove(i);
            }
        }
        if (f) {
            System.out.println("删除成功%n");
        } else {
            System.out.println("删除失败，请检查你的学号...");
        }
        save();
        System.out.println();
    }
}
